﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.Office.Interop.Excel;

namespace Org.Kaka.CsmsUtil.DataEI.Export
{
    public class ExcelExport:Export
    {
        private   Application app = null;
        private   Workbook workbook = null;
        private   Worksheet worksheet = null;
        private   Range workSheet_range = null;
        public  void CreateExcel()
        {
            app = new Microsoft.Office.Interop.Excel.ApplicationClass();
            app.Visible = false;
            workbook = app.Workbooks.Add(1);
            worksheet = (Worksheet)workbook.Sheets[1];            
        }

        public void AddData(int row,int col, string data, string format,string cell1,string cell2)
        {
            //worksheet.Cells[row, col] = data;
            workSheet_range = worksheet.get_Range(cell1, cell2);
            workSheet_range.Borders.Color = System.Drawing.Color.Orange.ToArgb();
            //workSheet_range.RowHeight = 100;
            //workSheet_range.Cells[2, 20] = 44;
            workSheet_range.Style = "color:red";
            workSheet_range.NumberFormat = format; 
        }

        public void export()
        {
            workbook.SaveCopyAs(@"D:/aa.xls");
            app.Quit();
        }

        public string toName(int index)
        {
            if (index <= 0)

                throw new Exception("invaild parameter");

            index--;

            List<string> chars = new List<string>();

            do
            {

                if (chars.Count > 0)

                    index--;

                chars.Insert(0, ((char)(index % 26 + (int)'A')).ToString());

                index = (int)((index - index % 26) / 26);

            } while (index > 0);



            return String.Join(string.Empty, chars.ToArray()); 
        }
    }
}
